package net.xdclass.online_xdclass.utils;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author kingtao
 * @date 2022/3/6
 */
@Component
public class BaseCache {
    private Cache<String, Object> tenMinuteCache = CacheBuilder.newBuilder()
            //  设置缓存初始大小
            .initialCapacity(10)
            //  最大值
            .maximumSize(100)
            //  并发数
            .concurrencyLevel(5)
            //  缓存时间，写入后6min过期
            .expireAfterWrite(600, TimeUnit.SECONDS)
            //  统计缓存的命中率
            .recordStats()

            .build();

    private Cache<String, Object> oneHourCache = CacheBuilder.newBuilder()
            //  设置缓存初始大小
            .initialCapacity(10)
            //  最大值
            .maximumSize(100)
            //  并发数
            .concurrencyLevel(5)
            //  缓存时间，写入后1小时过期
            .expireAfterWrite(1, TimeUnit.HOURS)
            //  统计缓存的命中率
            .recordStats()

            .build();

    public Cache<String, Object> getOneHourCache() {
        return oneHourCache;
    }

    public void setOneHourCache(Cache<String, Object> oneHourCache) {
        this.oneHourCache = oneHourCache;
    }

    public Cache<String, Object> getTenMinuteCache() {
        return tenMinuteCache;
    }

    public void setTenMinuteCache(Cache<String, Object> tenMinuteCache) {
        this.tenMinuteCache = tenMinuteCache;
    }
}
